import {
  formateDate
} from '../../../utils/date'
import {
  getToken
} from '../../../utils/getToken'
Page({
  data: {
    // 报修详情id
    // 如果是修改，则会初始化这个id。否则就是空字符串。
    id: '',
    mobile: '', // 手机号
    description: '', //问题描述
    attachment: [], // 上传图片
    repairHouseId: '', // 维修房屋id
    repairHouseName: '', // 维修房屋名称
    repairItemId: '', //报修项目ID
    repairItemName: '', //报修项目名称
    currentDate: Date.now(), // 当前时间
    appointment: '', // 维修时间
    isHouseLayerShow: false,
    isRepairLayerShow: false,
    isDateLayerShow: false,
    repairHouseList: [], // 报修房屋列表
    repairItemList: [] // 维修项目
  },
  onLoad(options) {
    console.log('报修表单页面接收的数据', options)
    // 判断是编辑还是新增
    if (options.houseId) {
      // 发起获取房屋报修信息请求
      this.getHouseRepairDetail(options.houseId)
    }
    this.getRepairHouseList()
    this.getRepairItemList()
  },
  getHouseRepairDetail(houseId) {
    wx.request({
      url: `https://live-api.itheima.net/repair/${houseId}`,
      method: 'GET',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        console.log('获取房屋报修详情res', res)
        if (res.data.code === 10000 && res.data.data) {
          // 回填表单数据
          this.setData({
            // 如果是编辑，id就会有值
            id: res.data.data.id,
            repairHouseId: res.data.data.houseId,
            repairHouseName: res.data.data.houseInfo,
            repairItemId: res.data.data.repairItemId,
            repairItemName: res.data.data.repairItemName,
            mobile: res.data.data.mobile,
            description: res.data.data.description,
            appointment: res.data.data.appointment,
            attachment: res.data.data.attachment,
          })
        }
      }
    })
  },
  // 上传图片
  handleImageAfterRead(e) {
    console.log('图片上传完毕', e)
    wx.uploadFile({
      // 上传图片本地路径、
      filePath: e.detail.file.url,
      // 图片上传接口参数
      name: 'file',
      // 图片上传接口
      url: 'https://live-api.itheima.net/upload',
      // 请求头
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      // 图片上传成功回调
      success: (res) => {
        console.log('图片上传结果', res)
        // 解析返回数据
        const data = JSON.parse(res.data)
        // 返回结果状态判断
        if (data.code !== 10000) {
          return wx.showToast({
            title: data.message,
            icon: 'none'
          })
        }
        // 追加上传成功图片到当前文件列表
        const newList = [...this.data.attachment, data.data]
        // 设置页面数据
        this.setData({
          attachment: newList
        })
      }
    })
  },
  // ·················
  handleRepairHouseSelect(e) {
    this.setData({
      repairHouseId: e.detail.id,
      repairHouseName: e.detail.name
    })
  },
  // 报修房屋
  getRepairHouseList() {
    wx.request({
      url: 'https://live-api.itheima.net/house',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        console.log('获取报修房屋列表res', res)
        if (res.data.code !== 10000) {
          return wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        this.setData({
          repairHouseList: res.data.data
        })
      }
    })
  },
  // ·······················
  handleRepairItemSelect(e) {
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name
    })
  },
  // 维修项目
  getRepairItemList() {
    wx.request({
      url: 'https://live-api.itheima.net/repairItem',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        console.log('获取列表res', res)
        if (res.data.code !== 10000) {
          return wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        this.setData({
          repairItemList: res.data.data
        })
      }
    })
  },
  // 报修房屋弹层
  openHouseLayer() {
    this.setData({
      isHouseLayerShow: true
    });
  },
  closeHouseLayer() {
    this.setData({
      isHouseLayerShow: false
    });
  },
  // 维修项目弹框
  openRepairLayer() {
    this.setData({
      isRepairLayerShow: true
    });
  },
  closeRepairLayer() {
    this.setData({
      isRepairLayerShow: false,
    });
  },
  // 预约日期
  openDateLayer() {
    this.setData({
      isDateLayerShow: true
    });
  },
  closeDateLayer() {
    this.setData({
      isDateLayerShow: false
    });
  },
  handleDateConfirm(e) {
    console.log('预约时间', e.detail)
    this.setData({
      isDateLayerShow: false,
      appointment: formateDate(e.detail)
    });
    console.log(this.data.appointment)
  },
  handleDateCancel() {
    this.setData({
      isDateLayerShow: false
    });
  },
  // 表单校验函数
  validateRepairData(data) {
    console.log('开始校验', data)
    // 报修房屋校验
    if (!data.repairHouseId.trim()) {
      wx.showToast({
        title: '请选择报修房屋',
        icon: 'none'
      })
      return false
    }
    // 保修项目校验
    if (!data.repairItemId.trim()) {
      wx.showToast({
        title: '请选择报修项目',
        icon: 'none'
      })
      return false
    }
    // 预约日期校验
    if (!data.appointment.trim()) {
      wx.showToast({
        title: '请选择预约日期',
        icon: 'none'
      })
      return false
    }
    // 问题描述校验
    if (!data.description.trim()) {
      wx.showToast({
        title: '请填写问题描述',
        icon: 'none'
      })
      return false
    }
    // 手机号码校验
    const mobileReg = /^[1][3-8][0-9]{9}$/
    if (!mobileReg.test(data.mobile.trim())) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return false
    }
    // 校验通过
    console.log('校验通过')
    return true
  },
  // 提交报修按钮
  handleRepairSubmit() {
    if (this.validateRepairData(this.data)) {
      wx.request({
        url: 'https://live-api.itheima.net/repair',
        method: 'post',
        header: {
          Authorization: 'Bearer ' + getToken()
        },
        data: {
          id: this.data.id,
          houseId: this.data.houseId,
          repairItemId: this.data.repairItemId,
          mobile: this.data.mobile,
          appointment: this.data.appointment,
          description: this.data.description,
          attachment: this.data.attachment
        },
        success: (res) => {
          console.log(this.data)
          console.log('提交成功', res)
          if (res.data.code !== 10000) {
            return wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
          wx.reLaunch({
            url: '/repair_pkg/pages/list/index',
          });
        }
      })
    }
  },
});